package com.imooc.bilibili.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.MediaType;

import java.util.Collections;

/**
 * @author gcq
 * @Create 2022-05-23
 */
@Configuration
public class JsonHttpMessageConvertConfig {

    /**
     * Json Http 消息转换器
     *
     * @return HttpMessageConverters
     */
    @Bean
    @Primary
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastConvert = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setDateFormat("YYYY-MM-dd HH:mm:ss");
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullStringAsEmpty, // 将空字符串写为空
                SerializerFeature.WriteNullListAsEmpty, // list 写为 null
                SerializerFeature.WriteMapNullValue, // map 写为 null
                SerializerFeature.MapSortField, // map 根据字段排序
                SerializerFeature.DisableCircularReferenceDetect // 禁止循环引用
        );
        fastConvert.setFastJsonConfig(fastJsonConfig);
        //如果使用feign进行微服务间的接口调用，则需要加上该配置
        fastConvert.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
        return new HttpMessageConverters(fastConvert);
    }




}